// pages/userInfo/renting/renting.js
const app = getApp()
import api from '../../../config/api'
import path from '../../../config/path'
const bubbleMixin = require('../../../utils/page-mixin');

Page({
  ...bubbleMixin,
  data: {
    rentingList: [],
    loading: false
  },

  onLoad() {
    this.loadData()
  },

  onShow() {
    this.loadData()
  },

  loadData() {
    const userInfo = wx.getStorageSync('userInfo')
    if (!userInfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return
    }
    this.setData({ loading: true })
    
    // 获取已同意的租房申请（status: 1）
    api.post(path.path.getMyApplies, { pageNo: 1, pageSize: 100, status: 1, userId: userInfo.id }).then(res => {
      if (res.code === 200) {
        this.setData({
          rentingList: res.data.list || []
        })
      }
    }).catch(err => {
      console.error('加载数据失败:', err)
    }).finally(() => {
      this.setData({ loading: false })
    })
  },

  viewHouseDetail(e) {
    const houseId = e.currentTarget.dataset.houseid
    wx.navigateTo({
      url: `/pages/index/houseInfo/houseInfo?id=${houseId}`
    })
  },

  onPullDownRefresh() {
    this.loadData()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  }
})

